//封装multer方法
const multer  = require('multer');
const path = require('path');
var sd = require('silly-datetime');
const mkdirp = require('mkdirp');
const md5 = require('md5');
let tools={
  multer(){
    const storage = multer.diskStorage({
      destination: function (req, file, cb) {
        //获取当前日期
        let day = sd.format(new Date(), 'YYYYMMDD');
        //static/upload/20211109
        let dir = path.join("static/upload",day);
        //借助mkdirp这个包快速生成文件夹
        mkdirp.sync(dir);
        cb(null, dir)
      },
      filename: function (req, file, cb) {
        //获取后缀名
        let extname=path.extname(file.originalname)
        cb(null, Date.now() + extname)
      }
    })
    const upload = multer({ storage: storage })
    return upload
  },
  md5(str){
    return md5(str);
  },
  getUnix(){//获取当前时间戳
    var d = new Date();
    return d.getTime();
  },
  formatTime(str){//格式化时间
    let day=sd.format(str, 'YYYY-MM-DD');
    return day;
  },
  unixToDay(unixStr){
    let day=sd.format(unixStr, 'DD');
    return day;
  },
  unixToYearAndMonth(unixStr){
    let day=sd.format(unixStr, 'YYYY-MM');
    return day;
  }
}
module.exports=tools
